import 'dart:io';

import 'package:furaha_mkopo/commonTool/fm_report_event_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_time_tool.dart';
import 'package:furaha_mkopo/commonUI/fm_progress_hud.dart';
import 'package:furaha_mkopo/commonUI/fm_toast.dart';
import 'package:furaha_mkopo/page/authentication/api/fm_auth_drew_api.dart';
import 'package:furaha_mkopo/page/authentication/api/fm_auth_image_upload_api.dart';
import 'package:furaha_mkopo/page/authentication/api/fm_identity_authentication_api.dart';
import 'package:furaha_mkopo/page/authentication/entity/fm_identity_authentication_entity.dart';
import 'package:furaha_mkopo/page/authentication/enum/fm_auth_select_enum.dart';
import 'package:furaha_mkopo/page/authentication/enum/fm_authentication_enum.dart';
import 'package:furaha_mkopo/page/home/api/fm_home_detail_api.dart';
import 'package:furaha_mkopo/page/home/entity/fm_home_detail_entity.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';

class FMAuthController extends GetxController {

  FMIdentityAuthenticationVoiceEntity? voice;
  Position? position;
  int startTime = 0;
  int faceTime = 0;
  @override
  void onInit() {
    super.onInit();
    startTime = FMTimeTool.currentTime;
  }

  void requestData(String listen) {
    FMProgressHud.showLoading();
    FMAuthDrewApi drewApi = FMAuthDrewApi(listen: listen);
    drewApi
        .send<FMIdentityAuthenticationEntity, FMIdentityAuthenticationEntity>(
            decodeType: FMIdentityAuthenticationEntity())
        .then((value) {
      FMProgressHud.hideLoading();
      if(value.data == null){
        FMToast.showText(value.message??"Network request error");
        return;
      }
      voice = value.data?.voice;
      update();
    });
  }

  void uploadImage(
      File file,
      FMAuthenticationEnum type,
      FMAuthSelectEnum selectEnum,
      String alighted,
      Function(FMAuthenticationEnum type, FMAuthImageResultEntity? resultEntity)
      onSuccess) {
    FMProgressHud.showLoading();
    String firm = "";
    switch (type) {
      case FMAuthenticationEnum.idCardFont:
      //走接口看返回信息弹信息确认窗，无问题走信息确认接口
        firm = "11";
        break;
      default:
      //调用接口上传
        firm = "10";
        break;
    }
    FMAuthImageUploadApi uploadApi =
    FMAuthImageUploadApi(image: file, firm: firm, alighted: alighted,card_type: selectEnum.title);
    switch (type) {
      case FMAuthenticationEnum.idCardFont:
        uploadApi
            .send<FMAuthImageResultEntity, FMAuthImageResultEntity>(
            decodeType: FMAuthImageResultEntity())
            .then((value) {
          FMProgressHud.hideLoading();
          if (value.data == null) {
            FMToast.showText(value.message ?? "Network request error");
            return;
          }
          onSuccess(type, value.data);
        });
        break;
      default:
        uploadApi.send().then((value) {
          FMProgressHud.hideLoading();
          if (value.data == null) {
            FMToast.showText(value.message ?? "Network request error");
            return;
          }
          onSuccess(type, null);
        });
        break;
    }
  }

  void onRefreshPage(String listen) {
    FMProgressHud.showLoading();
    FMIdentityAuthenticationApi api = FMIdentityAuthenticationApi(listen);
    api
        .send<FMIdentityAuthenticationEntity, FMIdentityAuthenticationEntity>(
        decodeType: FMIdentityAuthenticationEntity())
        .then((value) {
      FMProgressHud.hideLoading();
      if (value.data == null) {
        FMToast.showText(value.message ?? "Network request error");
        return;
      }
      voice = value.data!.voice;
      update();
    });
  }

  void reportRisk(String type,int startTime) async{
    await _getLocation();
    int endTime = FMTimeTool.currentTime;
    FMReportEventTool.reportRisk(type, position?.latitude, position?.longitude, startTime, endTime);
  }

  Future<void> _getLocation() async {
    LocationPermission permission = await Geolocator.checkPermission();

    if (permission == LocationPermission.whileInUse || permission == LocationPermission.always) {
      // 获取当前位置
      position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    } else {
      print('Location permission denied');
    }
  }
}
